/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package at.ac.tuwien.ewa.g70.api;

import java.io.IOException;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Amras
 */
@ManagedBean(name="loginManager")
@SessionScoped
public class LoginManager {
    
    @ManagedProperty(value="#{userManager}")
    UserManager userManager;

    @ManagedProperty(value="#{cardManager}")
    CardManager cardManager;

    private boolean errorOccured;
    private String errorMessage;

    private boolean loggedIn = false;

    private boolean needRegister = true;
    
    /*
     * the logged in user of the session
     */
    private User user;

    /*
     * indicates if the personal data form is displayed or not
     */
    @ManagedProperty(value = "false")
    private boolean personalData;

    /** Creates a new instance of LoginManager */
    public LoginManager() {
        this.user = new User();
    }

    public String getRequireLogin() throws IOException {
        if (!isLoggedIn()) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            facesContext.getExternalContext().redirect(
                    facesContext.getExternalContext().getRequestContextPath()
                        + "/faces/login.xhtml");
        }
        return "";
    }

    public String loginUser() {
        if( userManager.login(this.user.getLogin(), this.user.getPassword()) == null ){
            if ( user.getLogin() != null && user.getPassword() != null)
            this.errorOccured = true;
            this.needRegister = true;
            this.errorMessage = "Benutzername oder Passwort falsch";
            return "/login.xhtml";
        }
        else {
            this.user = userManager.getRegisteredUser(user.getLogin());
            this.initGame();
            this.errorOccured = false;
            this.needRegister = false;
            this.errorMessage = "";
            setLoggedIn(true);
            return "/table.xhtml";
        }
        
    }

    public void initGame(){
        int width = (int) Math.sqrt(this.user.getSizeOfStack());
        cardManager.init(CardManager.getDefaultCards(), width , width);
    }

    public String registerUser(){
       if( userManager.registerUser(this.user) ) {
           this.errorOccured = false;
           return "/login.xhtml";
       }
       else {
           this.errorOccured = true;
           return "/register.xhtml";
       }
    }

    public String changeUserdata(){
        if( userManager.changeUserdata(this.user) ) {
           this.initGame();
           return "/table.xhtml";
       }
       else {
           return "/userdata.xhtml";
       }
    }

    public User getUser() {
        return user;
    }

    public boolean isPersonalData() {
        return personalData;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setPersonalData(boolean personalData) {
        this.personalData = personalData;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    //Checks if the display checkbox changed
    public void displayChanged(ValueChangeEvent e){
        Boolean show = (Boolean) e.getNewValue();
        if(show != null)
            personalData = show;

        FacesContext.getCurrentInstance().renderResponse();
    }

    public CardManager getCardManager() {
        return cardManager;
    }

    public void setCardManager(CardManager cardManager) {
        this.cardManager = cardManager;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public boolean isErrorOccured() {
        return errorOccured;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setErrorOccured(boolean errorOccured) {
        this.errorOccured = errorOccured;
    }

    public Date getActualDate() {
        return new Date();
    }

    public boolean isNeedRegister() {
        return needRegister;
    }

    public void setNeedRegister(boolean needRegister) {
        this.needRegister = needRegister;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public String logout(){
        setLoggedIn(false);

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

        if (session != null) {
            session.invalidate();
        }

        return "/login.xhtml";
    }
}
